Completed
Push — master ( e3ee1d...5cbb1b )
by Justin
01:36
created

Person.setGender   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
rs 9.4285
1
var GedcomX = require('../'),
2
    utils = require('../utils');
3
    
4
/**
5
 * A person.
6
 * 
7
 * @constructor
8
 * @param {Object} [json]
0 ignored issues
show
Documentation introduced by
The parameter [json] does not exist. Did you maybe forget to remove this comment?
Loading history...
9
 */
10
var Person = function(json){
11
  
12
  // Protect against forgetting the new keyword when calling the constructor
13
  if(!(this instanceof Person)){
14
    return new Person(json);
15
  }
16
  
17
  // If the given object is already an instance then just return it. DON'T copy it.
18
  if(Person.isInstance(json)){
19
    return json;
20
  }
21
  
22
  this.init(json);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
23
};
24
25
Person.prototype = Object.create(GedcomX.Subject.prototype);
26
27
Person._gedxClass = Person.prototype._gedxClass = 'GedcomX.Person';
28
29
Person.jsonProps = [
30
  'private',
31
  'gender',
32
  'names',
33
  'facts'
34
];
35
36
/**
37
 * Check whether the given object is an instance of this class.
38
 * 
39
 * @param {Object} obj
40
 * @returns {Boolean}
41
 */
42
Person.isInstance = function(obj){
43
  return utils.isInstance(obj, this._gedxClass);
44
};
45
46
/**
47
 * Initialize from JSON
48
 * 
49
 * @param {Object}
0 ignored issues
show
Documentation introduced by
The parameter * does not exist. Did you maybe forget to remove this comment?
Loading history...
50
 * @return {Person} this
51
 */
52
Person.prototype.init = function(json){
53
  
54
  GedcomX.Subject.prototype.init.call(this, json);
55
  
56
  if(json){
57
    this.setPrivate(json.private);
58
    this.setGender(json.gender);
59
    this.setNames(json.names);
60
    this.setFacts(json.facts);
61
  }
62
  return this;
63
};
64
65
/**
66
 * Get the private flag
67
 * 
68
 * @returns {Boolean} private
69
 */
70
Person.prototype.getPrivate = function(){
71
  return this.private;
72
};
73
74
/**
75
 * Set the private flag
76
 * 
77
 * @param {Boolean} isPrivate
78
 * @returns {Person} This instance
79
 */
80
Person.prototype.setPrivate = function(isPrivate){
81
  this.private = isPrivate;
82
  return this;
83
};
84
85
/**
86
 * Get the person's gender
87
 * 
88
 * @returns {Gender} gender
89
 */
90
Person.prototype.getGender = function(){
91
  return this.gender;
92
};
93
94
/**
95
 * Set the person's gender
96
 * 
97
 * @param {Gender} gender
98
 * @returns {Person} This instance
99
 */
100
Person.prototype.setGender = function(gender){
101
  if(gender){
102
    this.gender = GedcomX.Gender(gender);
103
  }
104
  return this;
105
};
106
107
/**
108
 * Get the names
109
 * 
110
 * @return {Name[]}
111
 */
112
Person.prototype.getNames = function(){
113
  return this.names || [];
114
};
115
116
/**
117
 * Set the names
118
 * 
119
 * @param {Name[]|Object[]} names
120
 * @returns {Person} This instance
121
 */
122
Person.prototype.setNames = function(names){
123
  return this._setArray(names, 'names', 'addName');
124
};
125
126
/**
127
 * Add a name
128
 * 
129
 * @param {NameForm|Object} name
130
 * @returns {Person} This instance
131
 */
132
Person.prototype.addName = function(name){
133
  return this._arrayPush(name, 'names', GedcomX.Name);
134
};
135
136
/**
137
 * Get the facts
138
 * 
139
 * @return {Fact[]}
140
 */
141
Person.prototype.getFacts = function(){
142
  return this.facts || [];
143
};
144
145
/**
146
 * Set the facts
147
 * 
148
 * @param {Fact[]|Object[]} facts
149
 * @returns {Person} This instance
150
 */
151
Person.prototype.setFacts = function(facts){
152
  return this._setArray(facts, 'facts', 'addFact');
153
};
154
155
/**
156
 * Add a fact
157
 * 
158
 * @param {Fact|Object} fact
159
 * @returns {Person} This instance
160
 */
161
Person.prototype.addFact = function(fact){
162
  return this._arrayPush(fact, 'facts', GedcomX.Fact);
163
};
164
165
/**
166
 * Export the object as JSON
167
 * 
168
 * @return {Object} JSON object
169
 */
170
Person.prototype.toJSON = function(){
171
  return this._toJSON(GedcomX.Subject, Person.jsonProps);
172
};
173
174
module.exports = Person;